package vg.services.graph_view_manager.realization.graph_view.smart_graph_view.realization;

import java.util.Collection;

import vg.services.data_base_manager.data.graph.Attribute;
import vg.services.data_base_manager.data.graph.Edge;
import vg.services.data_base_manager.data.graph.Vertex;
import vg.services.graph_view_manager.interfaces.graph_view.ILowLevelEditGraphMethods;
import vg.services.graph_view_manager.realization.graph_view.smart_graph_view.interfaces.ISGVGraphView4Patching;
import vg.services.graph_view_manager.realization.graph_view.smart_graph_view.interfaces.ISGVPatchManager;
import vg.services.graph_view_manager.realization.graph_view.smart_graph_view.realization.patches.DefaultPatch;
import vg.services.graph_view_manager.realization.graph_view.smart_graph_view.shared.Utils;

/**
 * Implementation of ILowLevelEditGraphMethods.
 * @author Timur Zolotuhin (e-mail: tzolotuhin@gmail.com)
 */
public class SGVLowLevelGraphEditor implements ILowLevelEditGraphMethods {
	// Main components
	private ISGVPatchManager sgv_patch_manager;

	/**
	 * Constructor.
	 */
	public SGVLowLevelGraphEditor(ISGVPatchManager sgv_patch_manager) {
		this.sgv_patch_manager = sgv_patch_manager;
	}
	
	@Override
	public void setDirected(final boolean directed) {
		DefaultPatch patch = new DefaultPatch("Set directed") {
			@Override
			public void apply(ISGVGraphView4Patching graphView4patching) {
				graphView4patching.setDirected(directed);
			}
		};
		
		Utils.add_patch2patch_manager_and_wait_result(sgv_patch_manager, patch);
	}
	
	@Override
	public void addVertex(Vertex vertex) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void addVertices(Collection<Vertex> vertices) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void addEdge(Edge edge) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void addEdges(Collection<Edge> edges) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void addAttribute4Vertex(final Attribute attr, final Vertex vertex) {
		DefaultPatch patch = new DefaultPatch("Add attr for vertex") {
			@Override
			public void apply(ISGVGraphView4Patching graphView4patching) {
				graphView4patching.addAttribute4Vertex(attr, vertex);
			}
		};
		
		Utils.add_patch2patch_manager_and_wait_result(sgv_patch_manager, patch);	
	}
	
	@Override
	public void addAttribute4Edge(final Attribute attr, final Edge edge) {
		DefaultPatch patch = new DefaultPatch("Add attr for vertex") {
			@Override
			public void apply(ISGVGraphView4Patching graphView4patching) {
				graphView4patching.addAttribute4Edge(attr, edge);
			}
		};
		
		Utils.add_patch2patch_manager_and_wait_result(sgv_patch_manager, patch);	
	}
	
	@Override
	public int getGraphId() {
		DefaultPatch patch = new DefaultPatch("Get graph id") {
			@Override
			public void apply(ISGVGraphView4Patching graphView4patching) {
				result = graphView4patching.getGraphId();
			}
		};
		
		Utils.add_patch2patch_manager_and_wait_result(sgv_patch_manager, patch);		
		Object result = patch.getResult();
		if (result != null)
			return (Integer)result;
		return -1;
	}
	
	@Override
	public void setGraphId(final int graphId) {
		DefaultPatch patch = new DefaultPatch("Set graph id") {
			@Override
			public void apply(ISGVGraphView4Patching graphView4patching) {
				graphView4patching.setGraphId(graphId);
			}
		};
		
		Utils.add_patch2patch_manager_and_wait_result(sgv_patch_manager, patch);		
	}

	@Override
	public int getGraphModelId() {
		DefaultPatch patch = new DefaultPatch("Get graph model id") {
			@Override
			public void apply(ISGVGraphView4Patching graphView4patching) {
				result = graphView4patching.getGraphModelId();
			}
		};
		
		Utils.add_patch2patch_manager_and_wait_result(sgv_patch_manager, patch);		
		Object result = patch.getResult();
		if (result != null)
			return (Integer)result;
		return -1;
	}
	
	@Override
	public void setGraphModelId(final int graphModelId) {
		DefaultPatch patch = new DefaultPatch("Set graph id") {
			@Override
			public void apply(ISGVGraphView4Patching graphView4patching) {
				graphView4patching.setGraphId(graphModelId);
			}
		};
		
		Utils.add_patch2patch_manager_and_wait_result(sgv_patch_manager, patch);
	}
}
